// Sunghan Cho
// 301140084
// sunghanc@sfu.ca

#include <QtWidgets//QApplication>
#include <QDebug>

#include "mainwindow.h"
#include "datafileprocessor.h"
#include "unittests.h"

#include "repeatstudentlistmodel.h"
#include "selectedstudentlistmodel.h"
#include "uiitemtablewidget.h"
#include "studentdialog.h"

// ADD ANY NECESSARY INCLUDES HERE!


/*
  This file provides the basic construction code to startup the application.
  Follow the directions in the assignment description.

	Stages:
	0. Provided:
	   * MainWindow .h/.cpp
	   * main.cpp
	   * unittest.h/.cpp: Unit test functions
	   * CSV file processor (
	   * ui: itemtablewidget

	1. Implement: Item, Course and Student.
	   + Test routines to exercise these classes (main.cpp)
		 No GUI yet.
	2. Implement: Item manager
	   + Test routines
	   + Add provided MainWindow to project; dump-model button.
	   + Add DataFileProcessor to project & populate from file w/ GUI
	3. Implement: ItemListModel & UI Integration
	   + Display Courses on the screen, respond to updates (loading second file).
	4. {Selected, RepeatSelected}ListModels
	   + Display Students on the screen.
	5. Signals:
	   + Select course to filter students
	   + Clear course selection to return to normal.
	6. Implement student details dialog
	   + Select student to display/update dialog.
*/

int main(int argc, char *argv[])
{
	QApplication a(argc, argv);

	////////////////////////////////////
	// 1. Course & Student
	////////////////////////////////////
    testCourseData();
    testCourseItemInterface();
    testStudentData();
    testStudentItemInterface();


	////////////////////////////////////
	// 2. ItemManager
	////////////////////////////////////
	// NOTE: Change the "#if 0" to "#if 1" to enable the code.
#if 1
	// Call ItemManager test
    testItemManager();

	// Create Models
	ItemManager courses;
	ItemManager students;

	// Create GUI
	MainWindow w(&courses, &students);
	w.show();

	// Handle the MainWindow's "addItemsFromFile()" signal.
	// DataFileProcessor can read data from a .csv file into the
	// course and student models.
	DataFileProcessor fileProcessor(&courses, &students);
	QObject::connect(&w, SIGNAL(addItemsFromFile(QString)),
                     &fileProcessor, SLOT(processFile(QString)));
#endif

	////////////////////////////////////
	// 3. ItemListModel
	////////////////////////////////////
#if 1
	// Setup the list-model for the course table:
    ItemListModel *pCourseModel = new ItemListModel(&courses);
	// ...0's are for the two required student models (next step)
    w.setListModels(pCourseModel, 0, 0);
#endif

	////////////////////////////////////
	// 4. ItemListModel
	////////////////////////////////////
#if 0
	// Setup the list-model for the student tables:
	ItemListModel *pSelectedStudentModel = new SelectedStudentListModel(&students);
	ItemListModel *pRepeatStudentModel = new RepeatStudentListModel(&students);
	w.setListModels(pCourseModel, pSelectedStudentModel, pRepeatStudentModel);
#endif

	////////////////////////////////////
	// 5. Course Selection
	////////////////////////////////////
#if 0
    // Connect the item-selection SIGNAL from the course **ItemListModel**
    // To the setSelectedCourse SLOT of each of the student in **ItemListModel**

    QObject::connect(pCourseModel, SIGNAL(selectedItemChanged(Item*)), pSelectedStudentModel, SLOT(setSelectedCourse(Item*)));
#endif

	////////////////////////////////////
	// 6. Student Selection
    ////////////////////////////////////
	// Add your code here to:
	// 1. Instantiate a studentDialog
	// 2. Connect the student selection event from each
	//    student ItemListModel to the studentDialog.
#if 0
    StudentDialog s(&w);
    QObject::connect(pSelectedStudentModel, SIGNAL(selectedItemChanged(Item*)), &s, SLOT(setSelectedStudent(Item*)));
    QObject::connect(pRepeatStudentModel, SIGNAL(selectedItemChanged(Item*)), &s, SLOT(setSelectedStudent(Item*)));
#endif

	// Enter event loop:
	return a.exec();
}
